<?php 
/**************************************************/
## Class Name - Model_COUNTRY (Contains all the functions related Country modules)
## Created By - Techmodi (22/08/2011)
/**************************************************/
class Model_Leads extends Database
{	
	function Model_Leads() {	
		$this->leads = LEADS;		
		$this->state = STATE;
		$this->trade_cat = TRADE_CATEGORIES;
		$this->Database();
	}
	
	// for add state
	function addLeads($array) {
		$this->InsertData($this->leads, $array);
	}
	
	## List all state
	function getAllLeads($searchindex='',$col1='',$col2='',$col3='', $limit='',$offset='') {
		$fields = array('s.*','st.state_name','tr.trade_category');	
		$tables = array($this->leads." AS s",$this->state." AS st",$this->trade_cat." AS tr");
		$where= array("s.region_id=st.state_id and s.jobtype=tr.id");
		
		
		if($searchindex != '' && $searchindex!='Region Name') {
			$where[] = "st.state_name LIKE '%".trim($searchindex)."%'";
		}
		if($col1 =='0')
		{
			$order = array("state_name ASC");
		}
		if($col2 =='0')
		{
			$order = array("trade_category ASC");
		}
		if($col3=='0')
		{
			$order = array("price ASC");
		}
		if($col1 =='1')
		{
			$order = array("state_name DESC");
		}
		if($col2 =='1')
		{
			$order = array("trade_category DESC");
		}
		if($col3=='1')
		{
			$order = array("price DESC");
		}
		$result1 = $this->SelectData($fields,$tables, $where, $order , $group=array(""),$limit,$offset,0); 
		$result  = $this->FetchAll($result1); 
		return $result;
	}
	
	## Get state details by leadid
	function getLeadsDetailsById($leadid) {
		$fields = array();	
		$tables = array($this->leads);
		$result1 = $this->SelectData($fields,$tables, $where= array("lead_id=".$leadid), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		//print_r($result);
		return $result;
	}
	
	## Edit Lead by leadid
	function editLeadsById($array, $leadid){
		$this->UpdateData($this->leads,$array,"lead_id",$leadid,0);
	}
	
	
	## Delete lead by id
	function deleteLeadsById($leadid){
		$this->DeleteData($this->leads,"lead_id",$leadid);
	}
	
	
	
	## Delete leads with multiple leadids
	function deleteMultipleLeads($leadids) {

		$sql2 = "DELETE FROM ".$this->leads." WHERE lead_id IN (".$leadids.")";
		$result1= $this->ExecuteQuery($sql2);	 
	}
	
	## Get lead details by all data chk
	function getLeadsExits($stateid,$jobid,$budgetstr,$price) 	{
		$fields = array();	
		$tables = array($this->leads);
		$result1 = $this->SelectData($fields,$tables, $where= array("region_id=".$stateid." and jobtype =".$jobid." and budget ='".$budgetstr."' and price ='".$price."'"), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		//print_r($result);
		return $result;
	}
	
	## checking Lead exist or not 
	function getLeadsExitsORNot($stateid,$jobid,$budgetstr,$price,$postalcode, $id='') {
		$fields = array();	
		$tables = array($this->leads);
		$where= array("region_id=".$stateid." and jobtype =".$jobid." and budget ='".$budgetstr."' and price ='".$price."' and postalcode ='".$postalcode."'");
		
		if($id != '') {
			$where[] = "lead_id!=".$id;	
		}
		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		return $result;
	}
	
	
	## Get lead count by all job id
	function getchktradecategoryByJobID($jobid) 	{
		$fields = array("count(lead_id) leadcnt");	
		$tables = array($this->leads);
		$result1 = $this->SelectData($fields,$tables, $where= array("jobtype=".$jobid), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		//print_r($result);
		return $result;
	}
	
	## Get lead count by all state id
	function getchkstateBystateID($stateid) 	{
		$fields = array("count(lead_id) leadcnt");	
		$tables = array($this->leads);
		$result1 = $this->SelectData($fields,$tables, $where= array("region_id=".$stateid), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		//print_r($result);
		return $result;
	}

	## Get lead price by area, jobtype and budget
	function getLeadPriceByAreaJobTypeBudget($workarea,$jobtype,$budget) {
		$fields = array("");	
		$tables = array($this->leads);
		//$result1 = $this->SelectData($fields,$tables, $where= array("postalcode='".$workarea."' and jobtype='".$jobtype."' and  budget ='".$budget."'"), $order = array(), $group=array(),$limit = "",0,0);
		$result1 = $this->SelectData($fields,$tables, $where= array("jobtype='".$jobtype."' and  budget ='".$budget."'"), $order = array(), $group=array(),$limit = "",0,0);		
		$result  = $this->FetchRow($result1); 
		return $result;
	}	
	
}
?>